package pers.cz.db.redis;

import pers.cz.enums.RedisConfigItem;
import pers.cz.jefconfig.config.JefConfiguration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @program: postgirl-collection-client
 * @description: RedisUtil
 * @author: Cheng Zhi
 * @create: 2022-02-07 18:16
 **/
public class RedisConfig {

    public JedisPool redisPoolFactory() {

        Integer maxIdle = Integer.valueOf(JefConfiguration.get(RedisConfigItem.REDIS_MAXIDLE));
        Integer maxWait = Integer.valueOf(JefConfiguration.get(RedisConfigItem.REDIS_MAXWAIT));
        Boolean blockWhenExhausted = JefConfiguration.getBoolean(RedisConfigItem.REDIS_BLOCKWHENEXHAUSTED, true);
        String host = JefConfiguration.get(RedisConfigItem.REDIS_HOST);
        Integer port = JefConfiguration.getInt(RedisConfigItem.REDIS_PORT, 6039);
        Integer timeout = JefConfiguration.getInt(RedisConfigItem.REDIS_TIMEOUT, 1000);
        Integer maxActive = JefConfiguration.getInt(RedisConfigItem.REDIS_MAXACTIVE, 20);
        String password = JefConfiguration.get(RedisConfigItem.REDIS_PASSWORD);

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWait);
        jedisPoolConfig.setMaxTotal(maxActive);
        // 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(true);
        // Idle(空闲)时进行连接扫描
        jedisPoolConfig.setTestWhileIdle(true);
        // 表示idle object evitor两次扫描之间要sleep的毫秒数
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
        // 表示idle object evitor每次扫描的最多的对象数
        jedisPoolConfig.setNumTestsPerEvictionRun(10);
        // 表示一个对象至少停留在idle状态的最短时间，然后才能被idle object evitor扫描并驱逐；这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义
        jedisPoolConfig.setMinEvictableIdleTimeMillis(60000);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
        return jedisPool;

    }

}
